package cst.a15110100118yfx.fragmenttesty118yfx.Model;

import java.util.List;

import cst.a15110100118yfx.fragmenttesty118yfx.Bean.ArticleBean;
import cst.a15110100118yfx.fragmenttesty118yfx.iface.ArticleIface;
import cst.a15110100118yfx.fragmenttesty118yfx.iface.ArticleListener;
import cst.a15110100118yfx.fragmenttesty118yfx.service.Articleservice;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Activitymodel implements ArticleIface {

    private Retrofit retrofit;
    private static String BASE_URL="http://amicool.neusoft.edu.cn/";

    public Activitymodel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getLieBiaoList(String mod, int page, String sessionID, final ArticleListener listener) {
        Articleservice service = retrofit.create(Articleservice.class);

        Call<List<ArticleBean>> call = service.getResult(mod, page, sessionID);

        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {

                if(response.isSuccessful() &&
                        response.body() !=null){

                    listener.onResponse(response.body());
                }else
                    listener.onFail("NO");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
